﻿var SeoSlug = {};
SeoSlug.CreateSeoSlug = function(title)
{
	if (title == null || title.length == 0)
	{
		return "";
	}
	
	var slugRegex = /[^a-z0-9 ]*/g;
	var slugExtension = /\.aspx$/;
	var stopWords = new Array( 
	    "a", 
    	"b", 
    	"c", 
    	"d",
    	"e",
    	"f", 
    	"g", 
    	"h",
    	"i",
    	"j",
    	"k", 
    	"l", 
    	"m",
    	"n",
    	"o",
    	"p", 
    	"q", 
    	"r", 
    	"s", 
    	"t",
    	"u",
    	"v",
    	"w",
    	"x", 
    	"y",
    	"z" );
	
	var slug = title.toLowerCase();
	
	var slugWords = slug.split(/[,\s-_]/);
	slug = "";
	for (var i = 0; i < slugWords.length; i++)
	{
		var word = slugWords[i];		
		if (word.length > 0)
		{
			// remove extension if applicable
			word = word.replace(slugExtension, "");
			// remove special characters
			word = word.replace(slugRegex, "");
			
			if (word.length > 0)
			{
				if (!stopWords.contains(word))
				{
					if (slug.length > 0)
					{
						slug += "-";
					}
					
					slug += word;
				}
			}
		}
	}
	
	return slug;
}

Array.prototype.contains = function(element) 
{
	for (var i = 0; i < this.length; i++)
	{
		if (this[i] == element)
		{
			return true;
		}
	}
	return false;
};

function UpdateUrl()
{
	var urlNameTextBoxClientId = "ctl00_PlaceHolderMain_pageTitleSection_ctl02_urlNameTextBox";
	var urlTextBox = document.getElementById(urlNameTextBoxClientId);
	var titleTextBoxClientId = "ctl00_PlaceHolderMain_pageTitleSection_ctl00_titleTextBox";
	var titleTextBox = document.getElementById(titleTextBoxClientId);
	if (titleTextBox.value != "")
	{
		var seoSlug = SeoSlug.CreateSeoSlug(titleTextBox.value);
		seoSlug = seoSlug.substring(0,123);
		urlTextBox.value = seoSlug;
	}
}